package com.ykt.mall.ctrl;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.ykt.interceptor.Seach;
import com.ykt.model.Order;
import com.ykt.model.Product;
import com.ykt.model.Shop;

@ControllerBind(controllerKey="/cart")//viewPath="/template"
public class ShoppingCartCtrl extends Controller {
	
	public void view(){
		
		int shopId = isParaBlank(0) ? 0 : getParaToInt();
		System.out.println(isParaBlank(0) + "--->"+getParaToInt());
		
		String cartCookieProId = "";//取出放在cookie中的商品id
		
		List<Product> list = new ArrayList<Product>();		
		
		try{
			String cookie = getCookie("shop_"+String.valueOf(shopId));
			if(StringKit.notBlank(cookie)){
				cartCookieProId = URLDecoder.decode(cookie, "UTF-8");
				if(cartCookieProId != null){
					String[] proIds = cartCookieProId.split(",");
					for(String id : proIds){
						Product proPo = Product.dao.findById(Integer.valueOf(id));
						list.add(proPo);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		setAttr("shopPo", Shop.dao.findById(shopId));
		setAttr("productList", list);
		
	}
	
	public void book(){
		render("/template/book.html");
	}
	@Before(Seach.class)
	public void getOrderList(){
		Seach seach= getAttr("seachBean");
		
		int page = this.getParaToInt("pageindex");
		int rows = this.getParaToInt("pagesize");
		
		Order.dao.paginate(page, rows, seach);
	}
	
}
